#include "bsp_adc_relation.h"
#include "bsp_ntc.h"
#include "adc.h"

int32_t get_bat_current_by_converted_value(uint16_t converted_value)
{
    float temp_vol;
    uint32_t sensor_vol;
    int32_t ret_curr;
    
    temp_vol = converted_value* 1.0 * ADC_REF_VOL * RATE_K / ADC_RANGE;
    sensor_vol = (uint32_t)(temp_vol * (R43 + R45)) / R45;
//		printf("temp_vol:%d\n",sensor_vol);
    ret_curr = ((int32_t)(sensor_vol - HO_VREF_BAT) * RATE_K / HO_SENSITIVITY_BAT);
    return ret_curr;
}


uint32_t get_bat_voltage_by_converted_value(uint16_t converted_value)
{
    float temp_vol;
    uint32_t ret_vol;
    
    temp_vol = converted_value* 1.0 * ADC_REF_VOL * RATE_K / ADC_RANGE;
    
    temp_vol = temp_vol / VIN_VOL_GAIN;
    
    ret_vol = (uint32_t)((temp_vol * R371 / RIN + temp_vol) * (R364 + R365 + R366)) / R366;
    return ret_vol;
}


int32_t get_vin_voltage_by_converted_value(uint16_t converted_value)
{
    float temp_vol;
    int32_t ret_vol;
    
    temp_vol = converted_value* 1.0 * ADC_REF_VOL * RATE_K / ADC_RANGE;
    
    ret_vol = (uint32_t)(temp_vol * (R213 + R214 + R215)) / R215;
    
    return ret_vol;
}


int16_t get_temperature_out_board_by_converted_value(uint16_t converted_value)
{
    
    float temp_vol;
    int16_t ret_temp;
    uint32_t res;
    
    temp_vol = converted_value * 1.0 * ADC_REF_VOL * RATE_K / ADC_RANGE;
    
    res = (uint32_t)(((temp_vol / RATE_K) * R277 * THOUSAND) / (VDD33 - (temp_vol / RATE_K)));
    
    get_temper_data_int16_t(&ret_temp, res);
    
    return ret_temp;
}



int16_t get_temperature_in_board_by_converted_value(uint16_t converted_value)
{
    
    float temp_vol;
    int16_t ret_temp;
    uint32_t res;
    
    temp_vol = converted_value * 1.0 * ADC_REF_VOL * RATE_K / ADC_RANGE;
    
    res = (uint32_t)(((temp_vol / RATE_K) * R277 * THOUSAND) / (VDD33 - (temp_vol / RATE_K)));
    
    get_temp(&ret_temp, res);
    
    return ret_temp;
}




